package com.wy.business.market.apps.services;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wy.framework.constants.Constants;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.pojos.TSysAppCategroy;
import com.wy.framework.pojos.TSysAppOrginfo;
import com.wy.framework.pojos.TSysAppRole;
import com.wy.framework.pojos.TSysAppUser;
import com.wy.framework.pojos.TSysCategoryList;
import com.wy.framework.pojos.TSysFunction;
import com.wy.framework.pojos.TSysFunctionUpdatePending;
import com.wy.framework.pojos.TSysLocalNetwork;
import com.wy.framework.pojos.TSysUserinfo;
import com.wy.framework.services.AbstractBaseService;
import com.wy.component.dataConvert.BeanUtil;
import com.wy.framework.utils.pagenation.CurrentPageUtils;

/**
 * 项目名称：openportal
 * 类名称：AppManageService
 * 类描述：我的APP仓库_APP管理
 * 创建人：张旭星
 * 创建时间：2016-3-21 上午11:47:28
 * 修改人：张旭星
 * 修改时间：2016-3-21 上午11:47:28
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@Service
public class NewAuditService extends AbstractBaseService {

	private static final char[] object = null;
	private static Logger logger = LoggerFactory
			.getLogger(NewAuditService.class);

	public List list(String tab) throws ErrorSqlException {
		List params = new ArrayList();
		String stateSql = "";
		if(StringUtils.equals(tab, "tab1_1")){
			stateSql = " ORDER BY f.updatetime DESC ";
		}
		//审核中
		else if(StringUtils.equals(tab, "tab1_2"))
			stateSql = " AND F.STATE in ('0','2','4') ORDER BY f.updatetime DESC ";
		//通过
		else if(StringUtils.equals(tab, "tab1_3"))
			stateSql = " AND F.STATE in ('6') ORDER BY f.updatetime DESC ";
		//驳回
		else if(StringUtils.equals(tab, "tab1_4"))
			stateSql = " AND F.STATE in ('1','5') ORDER BY f.updatetime DESC ";
		return dataBaseDao.queryForList("t_sys_function.listNewAudit",null,stateSql);
	}
	
	public List queryAppInfoByFunctionId(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_FUNCTION.queryAppInfoByFunctionId",paramList);
	}
	
	public List queryAppInfoByFunctionIdForUpGuade(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_FUNCTION.queryAppInfoByFunctionIdForUpGuade",paramList);
	}
	
	public List queryPicUrl(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_FUNCTION.queryPicUrl",paramList);
	}
	
	public List querylocalNetwork(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_FUNCTION.querylocalNetwork",paramList);
	}
	
	public List queryUserByOrgInfo(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_USERINFO.queryUserByOrgInfo",paramList);
	}
	
	public List queryUserByOrgInfoPage(List<Object> paramList, Integer pageSize, Integer pageNum) throws Exception {
		
		return CurrentPageUtils.pageUtils(dataBaseDao, "T_SYS_USERINFO.queryUserByOrgInfo", pageSize, pageNum, paramList);
	}
	
	public List queryRoleByOrgInfo(List<Object> paramList) throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_ROLEINFO.queryRoleByOrgInfo",paramList);
	}
	
	public List queryRoleByOrgInfoPage(List<Object> paramList, Integer pageSize, Integer pageNum) throws Exception {
		
		return CurrentPageUtils.pageUtils(dataBaseDao, "T_SYS_ROLEINFO.queryRoleByOrgInfo", pageSize, pageNum, paramList);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List queryAllClassify() throws Exception {
		
		List list=new ArrayList();
		
		Map map=new HashMap<Object,List>();
		map.put("id", "1");
		map.put("text", "类别列表");
		map.put("children", dataBaseDao.queryForList("T_SYS_CLASSIFY.queryAllClassify"));
		list.add(map);
		return list;
	}

	public List queryInterfaceByClassifyId(List<Object> paramList) throws Exception {
	
		return dataBaseDao.queryForList("T_SYS_INTERFACE_INFO.queryInterfaceByClassifyId",paramList);
	}
	
	public List queryCategoryInfoAttrIsZero() throws Exception {
		
		return dataBaseDao.queryForList("T_SYS_CATEGORY_LIST.queryCategoryInfoAttrIsZero");
	}
	
	public List queryHasSelectedIntegerface(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryNoKeyForList("SELECT T.*,I.INTERFACENAME FROM T_SYS_INTERFACE_APP T,T_SYS_INTERFACE_INFO I WHERE T.FUNCTIONID = ? AND I.INTERFACEID=T.INTERFACEID ", paramList);
	}
	
	public List queryHasSelectedAppUser(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryForList("T_SYS_FUNCTION.queryHasSelectedAppUser", paramList);
	}	
	
	public List queryHasSelectedAppRole(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryForList("T_SYS_FUNCTION.queryHasSelectedAppRole", paramList);
	}	
	
	public List queryHasSelectedOrg(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryNoKeyForList("SELECT t.ORGCODE FROM T_SYS_APP_ORGINFO t WHERE t.functionid=?", paramList);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map> queryAllOrgToChecked(List<Object> paramList) throws ErrorSqlException, FileNotFoundException, IOException{
		return BeanUtil.listToTree(dataBaseDao.queryForList("T_SYS_FUNCTION.queryAllOrgToChecked",paramList), "id", "pid");
	}
	
	public List queryOldCategroyData(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryForList("T_SYS_CATEGORY_LIST.queryOldCategroyData", paramList);
	}
	
	public List modifyAppUpDownState(List<Object> paramList) throws ErrorSqlException{
		
		return  dataBaseDao.queryForList("T_SYS_FUNCTION.modifyAppUpDownState", paramList);
	}

	
	
	/**
	 * 
	 * Function Name               addAppAuditInfo                                   
	 * @param appId
	 * @param addNodesOrg
	 * @param addUserData
	 * @param addRoleData
	 * @param fenlei
	 * @param radioVal
	 * @param descriptionFour
	 * @return
	 * @throws Exception          		             
	 * @description 保存新应用审核信息数据
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-3-26 下午5:58:30           黄忠                     Initial
	 **********************************************************************
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean addAppAuditInfo(String appId,String addNodesOrg,String addUserData,
			String addRoleData,String fenlei,String radioVal,String descriptionFour,String net_setting,String[] ipAddress,String[] ipAddressDesc) throws Exception {
		boolean result=true;
		try {
			//redis里取登录人
			TSysUserinfo tSysUserinfo = loginUser();
			
			if(tSysUserinfo == null) {
				throw new Exception("NewAuditService addAppAuditInfo 172 : 取得用户信息为null!");
			}
			
			TSysFunction tSysFunction=(TSysFunction) this.dataBaseDao.load(TSysFunction.class, appId);
			if(StringUtils.isNotEmpty(net_setting)){
				//修改网络设置
				tSysFunction.setInternetConfig(net_setting);
				dataBaseDao.update(tSysFunction);
			}
			
			//保存局域网信息
			//清空(新应用待审，升级待审，验收待审则清空IP网段信息)
			String st=tSysFunction.getState();
			if(st.equals(Constants.NEW_APP_PENDING) || st.equals(Constants.UPDATE_PENDING) || st.equals(Constants.PENDING_APPROVAL)){
				List list = dataBaseDao.getHibernateTemplate().find(" from TSysLocalNetwork t where t.TSysFunction.functionId=? ", appId);
				dataBaseDao.getHibernateTemplate().deleteAll(list);
				if(ipAddress!=null && StringUtils.equals(net_setting, "1")){
					for(int i=0;i<ipAddress.length;i++){
						TSysLocalNetwork tSysLocalNetwork = new TSysLocalNetwork();
						tSysLocalNetwork.setAddress(ipAddressDesc[i]);
						tSysLocalNetwork.setIp(ipAddress[i]);
						tSysLocalNetwork.setIsavailable("1");
						tSysLocalNetwork.setNetworkid(UUID.randomUUID().toString());
						tSysLocalNetwork.setOperatorname(tSysUserinfo.getUserName());
						tSysLocalNetwork.setTSysFunction(tSysFunction);
						tSysLocalNetwork.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysLocalNetwork);
					}
				}
			}
			
			//先删除应用所有旧的授权信息数据
			TSysAppOrginfo appOrgDelete=new TSysAppOrginfo();
			appOrgDelete.setTSysFunction(tSysFunction);
			dataBaseDao.delete(appOrgDelete);
			
			TSysAppUser tSysAppUserDelete = new TSysAppUser();
			tSysAppUserDelete.setTSysFunction(tSysFunction);
			dataBaseDao.delete(tSysAppUserDelete);
			
			TSysAppRole tSysAppRoleDelete = new TSysAppRole();
			tSysAppRoleDelete.setTSysFunction(tSysFunction);
			dataBaseDao.delete(tSysAppRoleDelete);
			
			TSysAppCategroy tSysAppCategroyDelete = new TSysAppCategroy();
			tSysAppCategroyDelete.setTSysFunction(tSysFunction);
			dataBaseDao.delete(tSysAppCategroyDelete);
			
			//保存应用授权部门关系表数据
			if(StringUtils.isNotEmpty(addNodesOrg)){
				String[] addNodesOrgStr=addNodesOrg.split(",");
				for (int i = 0; i < addNodesOrgStr.length; i++) {
					
					TSysAppOrginfo appOrg=new TSysAppOrginfo();
					appOrg.setApporginfoid(UUID.randomUUID().toString());
					appOrg.setIsavailable(Constants.IS_AVAILABLE);
					appOrg.setOperatoraccount(tSysUserinfo.getUserId());
					appOrg.setOperatorname(tSysUserinfo.getUserName());
					appOrg.setOrgcode(addNodesOrgStr[i]);
					appOrg.setTSysFunction(tSysFunction);
					appOrg.setUpdatetime(new Timestamp(new Date().getTime()));
					dataBaseDao.save(appOrg);
				}
			}
			
			//保存应用授权用户关系表数据
			if(StringUtils.isNotEmpty(addUserData)){
				String[] addUserDataStr=addUserData.split(",");
				for (int i = 0; i < addUserDataStr.length; i++) {
					
					TSysAppUser tSysAppUser = new TSysAppUser();
					tSysAppUser.setAppuserid(UUID.randomUUID().toString());
					tSysAppUser.setIsavailable(Constants.IS_AVAILABLE);
					tSysAppUser.setOperatoraccount(tSysUserinfo.getUserId());
					tSysAppUser.setOperatorname(tSysUserinfo.getUserName());
					tSysAppUser.setTSysFunction(tSysFunction);
					tSysAppUser.setUpdatetime(new Timestamp(new Date().getTime()));
					tSysAppUser.setUserid(addUserDataStr[i]);
					dataBaseDao.save(tSysAppUser);
				}
			}
			
			//保存应用授权角色关系表数据
			if(StringUtils.isNotEmpty(addRoleData)) {
				String[] addRoleDataStr=addRoleData.split(",");
				for (int i = 0; i < addRoleDataStr.length; i++) {
					
					TSysAppRole tSysAppRole = new TSysAppRole();
					tSysAppRole.setApproleid(UUID.randomUUID().toString());
					tSysAppRole.setIsavailable(Constants.IS_AVAILABLE);
					tSysAppRole.setOperatoraccount(tSysUserinfo.getUserId());
					tSysAppRole.setOperatorname(tSysUserinfo.getUserName());
					tSysAppRole.setRoleid(addRoleDataStr[i]);
					tSysAppRole.setTSysFunction(tSysFunction);
					tSysAppRole.setUpdatetime(new Timestamp(new Date().getTime()));
					dataBaseDao.save(tSysAppRole);
				}
			}
			
			//保存推荐分类数据
			if(StringUtils.isNotEmpty(fenlei)) {
				
				String[] fenleiStr=fenlei.split(",");
				for (int i = 0; i < fenleiStr.length; i++) {
					
					String idOrName = fenleiStr[i];
					//包含cid 则是推荐分类
					if(idOrName.contains("cid=")) {
						
						String id=idOrName.split("\"")[1];
						TSysAppCategroy tSysAppCategroy = new TSysAppCategroy();
						tSysAppCategroy.setAppcategroyid(UUID.randomUUID().toString());
						tSysAppCategroy.setIsavailable(Constants.IS_AVAILABLE);
						tSysAppCategroy.setOperatoraccount(tSysUserinfo.getUserId());
						tSysAppCategroy.setOperatorname(tSysUserinfo.getUserName());
						TSysCategoryList tSysCategoryList = new TSysCategoryList();
						tSysCategoryList.setCategoryid(id);
						tSysAppCategroy.setTSysCategoryList(tSysCategoryList);
						tSysAppCategroy.setTSysFunction(tSysFunction);
						tSysAppCategroy.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysAppCategroy);
					}
					
					//包含name 则是临时分类  先新增到分类信息表 
					if(idOrName.contains("name=")){
						
						String uuid=UUID.randomUUID().toString();
						String name=idOrName.split("\"")[1];
						TSysCategoryList tSysCategoryList = new TSysCategoryList();
						tSysCategoryList.setAttribute(Constants.APP_TYPE_TEMP);
						tSysCategoryList.setCategoryid(uuid);
						tSysCategoryList.setCategoryname(name);
						tSysCategoryList.setIsavailable(Constants.IS_AVAILABLE);
						tSysCategoryList.setOperatoraccount(tSysUserinfo.getUserId());
						tSysCategoryList.setOperatorname(tSysUserinfo.getUserName());
						tSysCategoryList.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysCategoryList);
						
						TSysAppCategroy tSysAppCategroy = new TSysAppCategroy();
						tSysAppCategroy.setAppcategroyid(UUID.randomUUID().toString());
						tSysAppCategroy.setIsavailable(Constants.IS_AVAILABLE);
						tSysAppCategroy.setOperatoraccount(tSysUserinfo.getUserId());
						tSysAppCategroy.setOperatorname(tSysUserinfo.getUserName());
						TSysCategoryList tSysCategory = new TSysCategoryList();
						tSysCategory.setCategoryid(uuid);
						tSysAppCategroy.setTSysCategoryList(tSysCategory);
						tSysAppCategroy.setTSysFunction(tSysFunction);
						tSysAppCategroy.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysAppCategroy);
					}
				}
				
			}
			
			//根据应用ID查询应用信息
			tSysFunction.setAuditingTime(new Timestamp(System.currentTimeMillis()));
			tSysFunction.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			tSysFunction.setAuditor(tSysUserinfo.getUserName());
			tSysFunction.setAcceptanceResult(descriptionFour);
			//获取应用状态
			String state = tSysFunction.getState();
			
			//新应用待审 1 审核通过 0 审核不通过
			if(Constants.NEW_APP_PENDING.equals(state)) {
				if("1".equals(radioVal)) {
					tSysFunction.setState(Constants.PENDING_APPROVAL);
					tSysFunction.setUpDownState(Constants.ALREADY_ON_THE_SHELVES);
				}
				
				if("0".equals(radioVal)) {
					tSysFunction.setState(Constants.NEW_APP_REJECTED);
					tSysFunction.setUpDownState(Constants.UNDER_SHELF);
				}
			}else if(Constants.UPDATE_PENDING.equals(state)){
				if("1".equals(radioVal)) {
					tSysFunction.setState(Constants.PENDING_APPROVAL);
					tSysFunction.setUpDownState(Constants.ALREADY_ON_THE_SHELVES);
				}
				
				if("0".equals(radioVal)) {
					tSysFunction.setState(Constants.UPDATE_REJECTED);
					tSysFunction.setUpDownState(Constants.UNDER_SHELF);
				}
			}else if(Constants.PENDING_APPROVAL.equals(state)){
				if("1".equals(radioVal)) {
					tSysFunction.setState(Constants.ACCEPTANCE_BY);
					tSysFunction.setUpDownState(Constants.ALREADY_ON_THE_SHELVES);
				}
				
				if("0".equals(radioVal)) {
					tSysFunction.setState(Constants.ACCEPTANCE_REJECTION);
					tSysFunction.setUpDownState(Constants.UNDER_SHELF);
				}
			}
			
			//修改应用信息
			dataBaseDao.getHibernateTemplate().update(tSysFunction);
//			dataBaseDao.update("T_SYS_FUNCTION.updateFunctionState", paramList);
			if(Constants.UPDATE_PENDING.equals(state) && ("1".equals(radioVal))){
				//升级待审通过时，function表更新升级数据
				result=updateAppUpGuade(tSysFunction);
			}
			
			return result;
		} catch (Exception e) {
			throw e;
			
		}
	}
	
	/**
	 * 
	 * Function Name               updateAppInfo                                   
	 * @param appId
	 * @param addNodesOrg
	 * @param addUserData
	 * @param addRoleData
	 * @param fenlei
	 * @param radioVal
	 * @param ipInfo
	 * @return
	 * @throws Exception          		             
	 * @description 安全设置
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-4-7 上午10:25:13           黄忠                     Initial
	 **********************************************************************
	 */
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean updateAppInfo(String appId,String addNodesOrg,String addUserData,
			String addRoleData,String fenlei,String radioVal,String ipInfo,String disabledFlg) throws Exception {
		
		try {
			//redis里取登录人
			TSysUserinfo tSysUserinfo = loginUser();
			
			if(tSysUserinfo == null) {
				throw new Exception("NewAuditService addAppAuditInfo 172 : 取得用户信息为null!");
			}
			
			TSysFunction tSysFunction=new TSysFunction();
			tSysFunction.setFunctionId(appId);
			
			//先删除应用所有旧的授权信息数据
//			TSysAppOrginfo appOrgDelete=new TSysAppOrginfo();
//			appOrgDelete.setTSysFunction(tSysFunction);
//			dataBaseDao.delete(appOrgDelete);
			
			dataBaseDao.getJdbcTemplate().execute("DELETE FROM t_sys_app_orginfo T WHERE T.FUNCTIONID = '" + appId+"'");

			
//			TSysAppUser tSysAppUserDelete = new TSysAppUser();
//			tSysAppUserDelete.setTSysFunction(tSysFunction);
//			dataBaseDao.delete(tSysAppUserDelete);
			
			dataBaseDao.getJdbcTemplate().execute("DELETE FROM t_sys_app_user T WHERE T.FUNCTIONID = '" + appId+"'");

			
//			TSysAppRole tSysAppRoleDelete = new TSysAppRole();
//			tSysAppRoleDelete.setTSysFunction(tSysFunction);
//			dataBaseDao.delete(tSysAppRoleDelete);
			
			dataBaseDao.getJdbcTemplate().execute("DELETE FROM t_sys_app_role T WHERE T.FUNCTIONID = '" + appId+"'");

			dataBaseDao.getJdbcTemplate().execute("DELETE FROM T_SYS_APP_CATEGROY T WHERE T.FUNCTIONID = '" + appId+"'");
//			TSysAppCategroy tSysAppCategroyDelete = new TSysAppCategroy();
//			tSysAppCategroyDelete.setTSysFunction(tSysFunction);
//			dataBaseDao.delete(tSysAppCategroyDelete);
			
			//删除旧的IP地址信息
			List pamaList = new ArrayList();
			dataBaseDao.getJdbcTemplate().execute("DELETE FROM T_SYS_LOCAL_NETWORK T WHERE T.FUNCTIONID = '" + appId+"'");
			
			//保存应用授权部门关系表数据
			if(StringUtils.isNotEmpty(addNodesOrg)){
				String[] addNodesOrgStr=addNodesOrg.split(",");
				for (int i = 0; i < addNodesOrgStr.length; i++) {
					
					TSysAppOrginfo appOrg=new TSysAppOrginfo();
					appOrg.setApporginfoid(UUID.randomUUID().toString());
					appOrg.setIsavailable(Constants.IS_AVAILABLE);
					appOrg.setOperatoraccount(tSysUserinfo.getUserId());
					appOrg.setOperatorname(tSysUserinfo.getUserName());
					appOrg.setOrgcode(addNodesOrgStr[i]);
					appOrg.setTSysFunction(tSysFunction);
					appOrg.setUpdatetime(new Timestamp(new Date().getTime()));
					dataBaseDao.save(appOrg);
				}
			}
			
			//保存应用授权用户关系表数据
			if(StringUtils.isNotEmpty(addUserData)){
				String[] addUserDataStr=addUserData.split(",");
				for (int i = 0; i < addUserDataStr.length; i++) {
					
					TSysAppUser tSysAppUser = new TSysAppUser();
					tSysAppUser.setAppuserid(UUID.randomUUID().toString());
					tSysAppUser.setIsavailable(Constants.IS_AVAILABLE);
					tSysAppUser.setOperatoraccount(tSysUserinfo.getUserId());
					tSysAppUser.setOperatorname(tSysUserinfo.getUserName());
					tSysAppUser.setTSysFunction(tSysFunction);
					tSysAppUser.setUpdatetime(new Timestamp(new Date().getTime()));
					tSysAppUser.setUserid(addUserDataStr[i]);
					dataBaseDao.save(tSysAppUser);
				}
			}
			
			//保存应用授权角色关系表数据
			if(StringUtils.isNotEmpty(addRoleData)) {
				String[] addRoleDataStr=addRoleData.split(",");
				for (int i = 0; i < addRoleDataStr.length; i++) {
					
					TSysAppRole tSysAppRole = new TSysAppRole();
					tSysAppRole.setApproleid(UUID.randomUUID().toString());
					tSysAppRole.setIsavailable(Constants.IS_AVAILABLE);
					tSysAppRole.setOperatoraccount(tSysUserinfo.getUserId());
					tSysAppRole.setOperatorname(tSysUserinfo.getUserName());
					tSysAppRole.setRoleid(addRoleDataStr[i]);
					tSysAppRole.setTSysFunction(tSysFunction);
					tSysAppRole.setUpdatetime(new Timestamp(new Date().getTime()));
					dataBaseDao.save(tSysAppRole);
				}
			}
			
			//保存推荐分类数据
			if(StringUtils.isNotEmpty(fenlei)) {
				
				String[] fenleiStr=fenlei.split(",");
				for (int i = 0; i < fenleiStr.length; i++) {
					
					String idOrName = fenleiStr[i];
					//包含cid 则是推荐分类
					if(idOrName.contains("cid=")) {
						
						String id=idOrName.split("\"")[1];
						TSysAppCategroy tSysAppCategroy = new TSysAppCategroy();
						tSysAppCategroy.setAppcategroyid(UUID.randomUUID().toString());
						tSysAppCategroy.setIsavailable(Constants.IS_AVAILABLE);
						tSysAppCategroy.setOperatoraccount(tSysUserinfo.getUserId());
						tSysAppCategroy.setOperatorname(tSysUserinfo.getUserName());
						TSysCategoryList tSysCategoryList = new TSysCategoryList();
						tSysCategoryList.setCategoryid(id);
						tSysAppCategroy.setTSysCategoryList(tSysCategoryList);
						tSysAppCategroy.setTSysFunction(tSysFunction);
						tSysAppCategroy.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysAppCategroy);
					}
					
					//包含name 则是临时分类  先新增到分类信息表 
					if(idOrName.contains("name=")){
						
						String uuid=UUID.randomUUID().toString();
						String name=idOrName.split("\"")[1];
						TSysCategoryList tSysCategoryList = new TSysCategoryList();
						tSysCategoryList.setAttribute(Constants.APP_TYPE_TEMP);
						tSysCategoryList.setCategoryid(uuid);
						tSysCategoryList.setCategoryname(name);
						tSysCategoryList.setIsavailable(Constants.IS_AVAILABLE);
						tSysCategoryList.setOperatoraccount(tSysUserinfo.getUserId());
						tSysCategoryList.setOperatorname(tSysUserinfo.getUserName());
						tSysCategoryList.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysCategoryList);
						
						TSysAppCategroy tSysAppCategroy = new TSysAppCategroy();
						tSysAppCategroy.setAppcategroyid(UUID.randomUUID().toString());
						tSysAppCategroy.setIsavailable(Constants.IS_AVAILABLE);
						tSysAppCategroy.setOperatoraccount(tSysUserinfo.getUserId());
						tSysAppCategroy.setOperatorname(tSysUserinfo.getUserName());
						TSysCategoryList tSysCategory = new TSysCategoryList();
						tSysCategory.setCategoryid(uuid);
						tSysAppCategroy.setTSysCategoryList(tSysCategory);
						tSysAppCategroy.setTSysFunction(tSysFunction);
						tSysAppCategroy.setUpdatetime(new Timestamp(new Date().getTime()));
						dataBaseDao.save(tSysAppCategroy);
					}
				}
				
			}
			
			//保存新的IP和地址信息
			if("1".equals(radioVal) && StringUtils.isNotEmpty(ipInfo)) {
				
				String[] ipAndAddrArr=ipInfo.split(",");
				for (int i = 0; i < ipAndAddrArr.length; i++) {
					String ipAndAddr = ipAndAddrArr[i];
					String ip = ipAndAddr.substring(0,ipAndAddr.indexOf("|"));
					String addr = ipAndAddr.substring(ipAndAddr.indexOf("|")+1, ipAndAddr.length());
					
					TSysLocalNetwork tSysLocalNetwork = new TSysLocalNetwork();
					 
					 tSysLocalNetwork.setAddress(addr);
					 tSysLocalNetwork.setIp(ip);
					 tSysLocalNetwork.setIsavailable(Constants.IS_AVAILABLE);
					 tSysLocalNetwork.setNetworkid(UUID.randomUUID().toString());
					 tSysLocalNetwork.setOperatoraccount(tSysUserinfo.getUserId());
					 tSysLocalNetwork.setOperatorname(tSysUserinfo.getUserName());
					 tSysLocalNetwork.setTSysFunction(tSysFunction);
					 tSysLocalNetwork.setUpdatetime(new Timestamp(new Date().getTime()));
					 dataBaseDao.save(tSysLocalNetwork);
				}
			}
			
			pamaList = new ArrayList();
			pamaList.add(radioVal);
			pamaList.add(disabledFlg);
			pamaList.add(appId);
			dataBaseDao.update("T_SYS_FUNCTION.modifyAppNetInfo", pamaList);
			return true;
		} catch (Exception e) {
			throw e;
		}
		
	}
	
	/**
	 * Function Name               updateAppUpGuade                                   
	 * @param functionId
	 * @return
	 * @throws Exception 
	 * @description TODO(function表更新升级数据)
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-4-5 下午5:20:33          朱仕稳                     Initial
	 **********************************************************************
	 */
	public boolean updateAppUpGuade(TSysFunction t) throws Exception{
		//redis里取登录人
		TSysUserinfo tSysUserinfo = loginUser();
		//更新升级应用数据
		
		//查询升级内容（升级待审表）,替换Function表中的数据为升级的数据
		List list=this.dataBaseDao.getHibernateTemplate().find("FROM TSysFunctionUpdatePending WHERE FUNCTIONID=?", t.getFunctionId());
		if(list==null || list.size()==0 || list.get(0)==null){
			return false;
		}
		TSysFunctionUpdatePending tp=(TSysFunctionUpdatePending) list.get(0);
		t.setActionUrl(tp.getActionurl());
		t.setAppSize(tp.getAppsize());
		t.setAppZipUrl(tp.getAppzipurl());
		t.setDeploymentType(tp.getDeploymenttype());
		t.setDescription(tp.getDescription());
		t.setFunctionName(tp.getFunctionname());
		t.setFunctionNameEn(tp.getFunctionnameen());
		t.setIconName(tp.getIconname());
		t.setManageUrl(tp.getManageurl());
		t.setOperatorAccount(tp.getOperatoraccount());
		t.setOperatorName(tSysUserinfo.getUserName());
		t.setProvider(tSysUserinfo.getUserName());
		t.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		t.setUpgradeContent(tp.getUpgradecontent());
		t.setVersionName(tp.getVersionname());
		t.setVersionNo(tp.getVersionno());
		dataBaseDao.getHibernateTemplate().update(t);
		
		//删除升级待审表中的数据
		dataBaseDao.delete(tp);
		return true;
	} 
	
}
